package com.financing.platform.util;

import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

public class AliyunMobileQueryUtil {
	public static String host = "https://api04.aliyun.venuscn.com";
	public static String path = "/mobile";
	public static String method = "GET";
	public static String appcode = "2fb1b0b243b444ca9f8002b4eac840cc";

	//10001	手机号码格式错误或少于7位
	//10002	查询号段不存在
	public  static JSONObject mobileQuery(String mobile){
		JSONObject json = null;
		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Authorization", "APPCODE " + appcode);
		Map<String, String> querys = new HashMap<String, String>();
		querys.put("mobile", mobile);


		try {

			HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
			String entity = EntityUtils.toString(response.getEntity());
			if(StringUtils.isNotBlank(entity)) {
				json = JSONObject.fromObject(entity);
			}else{
				json  =  new JSONObject();
				json.put("ret","10003");
				json.put("msg","请联系管理员");
			}

		} catch (Exception e) {
			e.printStackTrace();
			json  =  new JSONObject();
			json.put("ret","10003");
			json.put("msg","请联系管理员");
		}
		return json;
	}

	public static void main(String[] args) {
		JSONObject json = AliyunMobileQueryUtil.mobileQuery("15000774677");
		//json = JSONObject.fromObject(json.get("resp"));
		System.out.println("json = " + json.toString());

	}
}
